﻿MainScript = function () { };

MainScript.isNull = function (obj) {
    if (obj == null || typeof obj == undefined) {
        return true;
    }
    return false;
}

MainScript.isNullOrEmpty = function (obj) {
    if (this.isNull(obj)) {
        return true;
    }

    if (typeof obj == "string" && obj.Trim() == "") {
        return true;
    }

    return false;
}

MainScript.isIE = function () {
    return window.navigator.userAgent.indexOf("MSIE") >= 1;
}

MainScript.getIEVersion = function () {
    var versionInfo = window.navigator.appVersion.split("MSIE");
    var version = parseFloat(versionInfo[1]);
    return version;
}

MainScript.loadXMLDoc = function (url) {
    var xmlDoc;
    try {
        if (window.ActiveXObject) {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = false;
            xmlDoc.load(url);
            isIE = true;
            return xmlDoc;
        }
        else if (document.implementation && document.implementation.createDocument) {
            xmlDoc = document.implementation.createDocument("", "", null);
            xmlDoc.async = false;
            xmlDoc.load(url);
            return xmlDoc;
        }
    }
    catch (e) {
        try {
            var oXmlHttp = new XMLHttpRequest();
            oXmlHttp.open("GET", url, false);
            oXmlHttp.send(null);
            var str = oXmlHttp.responseText;
            return (new DOMParser()).parseFromString(str, "text/xml");
        } catch (e) {
            return;
        }
    }
    xmlDoc.async = false;
    xmlDoc.load(url);
    return xmlDoc;
}

MainScript.isCompatModeStandard = function () {
    return (document.compatMode == 'CSS1Compat');
}

MainScript.isSafari = function () {
    return window.navigator.userAgent.indexOf("Safari") >= 1;
}

MainScript.getClientHeight = function (target) {
    target = target || document;

    if (MainScript.isCompatModeStandard()) 
        return target.documentElement.clientHeight;
    else 
        return target.body.clientHeight;
}

MainScript.getScrollHeight = function (target) {

    target = target || document;

    if (MainScript.isCompatModeStandard()) {
        if (MainScript.isSafari())
            return target.body.scrollHeight;
        else
            return target.documentElement.scrollHeight;
    } else {
        return target.body.scrollHeight;
    }
}

MainScript.getClientWidth = function (target) {

    target = target || document;

    if (MainScript.isCompatModeStandard())
        return target.documentElement.clientWidth;
    else
        return target.body.clientWidth;
}

MainScript.getScrollWidth = function (target) {

    target = target || document;

    if (MainScript.isCompatModeStandard()) {
        if (MainScript.isSafari())
            return target.body.scrollWidth;
        else
            return target.documentElement.scrollWidth;
    } else {
        return target.body.scrollWidth;
    }
}

MainScript.getScrollTop = function (target) {

    target = target || document;

    if (MainScript.isCompatModeStandard()) {
        if (MainScript.isSafari())
            return target.body.scrollTop;
        else
            return target.documentElement.scrollTop;
    } else {
        return target.body.scrollTop;
    }
}

MainScript.getScrollLeft = function (target) {

    target = target || document;

    if (MainScript.isCompatModeStandard()) {
        if (MainScript.isSafari())
            return target.body.scrollLeft;
        else
            return target.documentElement.scrollLeft;
    } else {
        return target.body.scrollLeft;
    }
}

MainScript.getElementPosition = function (source) {
    var pt = { x: 0, y: 0 };
    do {
        if (source == null) {
            break;
        }
        pt.x += source.offsetLeft + MainScript.stringToInt(Element.getStyle(source, "borderLeftWidth"));
        pt.y += source.offsetTop + MainScript.stringToInt(Element.getStyle(source, "borderTopWidth"));
        source = source.offsetParent;
    }
    while (source);

    return pt;
}

MainScript.stringToInt = function (strNumber) {
    var value = parseInt(strNumber, 10);
    value = isNaN(value) ? 0 : value;
    return value;
}

String.prototype.Trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

MainScript.getBytesCount = function (target) {
    if (MainScript.isNullOrEmpty(target)) {
        return 0;
    }
    else {
        return (target.length + target.replace(/[\u0000-\u00ff]/g, "").length);
    }
}

//特殊字符转换处理
MainScript.encodeValue = function (v) {
    if (v.indexOf("%") != -1) {
        v = v.replaceAll("%", "%25");
    }

    if (v.indexOf("&") != -1) {
        v = v.replaceAll("&", "%26");
    }
    while (v.indexOf("+") != -1) {
        v = v.replace('+', "%2B");
    }

    return v;
}